<?php

namespace App\Api\Requests;

use App\Models\AdminUser;
use Illuminate\Validation\Rule;

class AdminUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->route('adminUser');//获取当前需要排除的id,这里的 adminUser 是 路由put {} 中的参数

        switch ($this->method()) {
            // CREATE
            case 'POST':
            {
                return [
                    'username' => "required|min:5|unique:admin_users",
                    'password' => 'required|min:6',
                    'name'     => 'required|min:5',
                    'status'   => [
                        'required',
                        Rule::in(array_keys(AdminUser::STATUS_MAP)),
                    ],
                ];
            }
            // UPDATE
            case 'PUT':
                $row = $this->route('id');
                return [
                    'username' => "required|min:5|unique:admin_users,username,{$id}",//table,column,except,idColumn
//                    'password' => 'required|min:6',
                    'name'     => 'required|min:5',
                    'status'   => [
                        'required',
                        Rule::in(array_keys(AdminUser::STATUS_MAP)),
                    ],
                ];
        }
    }
}
